<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Convert uint8array to double in javascript 将 uint8array 转换为双倍</title>
</head>
<body>
I have an arraybuffer and I want to get double values.For example from [64, -124, 12, 0, 0, 0, 0, 0] I would get 641.5
Any ideas?

回答1:
You could adapt the excellent answer of T.J. Crowder and use DataView#setUint8 for the given bytes.
<script>
  var data =  [64, -124, 12, 0, 0, 0, 0, 0];

  // Create a buffer
  var buf = new ArrayBuffer(8);
  // Create a data view of it
  var view = new DataView(buf);

  // set bytes
  data.forEach(function (b, i) {
    view.setUint8(i, b);
  });

  // Read the bits as a float/native 64-bit double
  var num = view.getFloat64(0);
  // Done
  console.log(num);
</script>
For multiple numbers, you could take chunks of 8.
<script>
  function getFloat(array) {
    var view = new DataView(new ArrayBuffer(8));
    array.forEach(function (b, i) {
      view.setUint8(i, b);
    });
    return view.getFloat64(0);
  }

  var data =  [64, -124, 12, 0, 0, 0, 0, 0, 64, -124, 12, 0, 0, 0, 0, 0],
    i = 0,
    result = [];

  while (i < data.length) {
    result.push(getFloat(data.slice(i, i + 8)));
    i += 8;
  }

  console.log(result);
</script>
回答2:
Based on the answer from Nina Scholz I came up with a shorter:
<script>
  function getFloat(data /* Uint8Array */) {
    return new DataView(data.buffer).getFloat64(0);
  }
</script>
Or if you have a large array and know the offset:
<script>
  function getFloat(data, offset = 0) {
    return new DataView(data.buffer, offset, 8).getFloat64(0);
  }
</script>
</body>
</html>
